package com.hrm.action;

import java.io.IOException;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hrm.config.MsgType;
import com.hrm.entity.Message;
import com.hrm.entity.User;
import com.hrm.service.UserLogService;
import com.hrm.service.UserService;
import com.hrm.util.JSONUtil;
import com.hrm.util.JspName;


@Controller
@RequestMapping("/login.do")
public class LoginAction {
	private static final Log log = LogFactory.getLog(LoginAction.class); 
	@Autowired
	private UserService userService;
	@Autowired
	private UserLogService userLogService;
	public UserLogService getUserLogService() {
		return userLogService;
	}
	public void setUserLogService(UserLogService userLogService) {
		this.userLogService = userLogService;
	}
	public UserService getUserService() {
		return userService;
	}
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	@RequestMapping(params = "method=login")
	public String login(HttpServletRequest request,
			HttpServletResponse response, ModelMap map){
		//登陆
		User user=userService.login(request.getParameter("username"), request.getParameter("password"));
		if(user!=null){
			log.info("begin to login");
			request.getSession().setAttribute("user", user);
			try {
				Message message = new Message("OK", "登陆成功!", new Date());
				ActionUtil.insertLog(request, MsgType.TYPE_LOGIN, "登陆成功",userLogService);
				response.getWriter().print(message.toJSONString());
			} catch (IOException e) {
				e.printStackTrace();
			}
		}else{
			try {
				Message message = new Message("ERROR", "用户名或密码错误!", new Date());
				response.getWriter().print(message.toJSONString());
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
		
	}
	@RequestMapping(params = "method=logout")
	public String logout(HttpServletRequest request,
			HttpServletResponse response, ModelMap map){
		try {
			ActionUtil.insertLog(request, MsgType.TYPE_LOGIN, "登陆状态:退出登陆",userLogService);
			request.getSession().removeAttribute("user");
			response.sendRedirect(request.getContextPath()+"/login.jsp");
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
		
	}
	@RequestMapping(params = "method=getPermissions")
	public String getPermissions(HttpServletRequest request,
			HttpServletResponse response, ModelMap map){
		log.info("get Permissions");
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		if(user==null){//判断是否未登陆
			try {
				response.sendRedirect(JspName.LOGINPAGE);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		String result=JSONUtil.parseList(userService.getPermission(user.getId()));
		try {
			response.getWriter().print(result);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
		
	}
}
